package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.dto.h5.ProductSkuSearchDto;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.result.Result;
import com.atguigu.spzx.model.vo.h5.ProductSkuDetailVo;
import com.atguigu.spzx.product.service.ProductSkuService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "商品模块")
@RequestMapping("/api/product")
public class ProductController {
    @Resource
    ProductSkuService productSkuService;
    @Operation(summary = "更新销量库存")
    @PutMapping("updateSalesAndStock/{skuId}/{skuNum}")
    public Result  updateSalesAndStock(@PathVariable("skuId")Long skuId,
                                       @PathVariable("skuNum")Integer skuNum){
        ProductSku productSku = productSkuService.getById(skuId);
        productSku.setStockNum(productSku.getStockNum()-skuNum);
        productSku.setSaleNum(productSku.getSaleNum()+skuNum);
        productSkuService.updateById(productSku);
        return Result.ok();
    }
    @Operation(summary = "根据id查询sku详情")
    @GetMapping("getById/{skuId}")
    public Result getById(@PathVariable("skuId")Long skuId){
        return Result.ok(productSkuService.getById(skuId));
    }
    @Operation(summary = "查询sku商品详情")
    @GetMapping("item/{skuId}")
    public Result findItem(@PathVariable("skuId")Long skuId){
        ProductSkuDetailVo productSkuDetailVo = productSkuService.findItem(skuId);
        return Result.ok(productSkuDetailVo);
    }

    //http://localhost:8888/api/product/1/10
    // ?category1Id=1&category2Id=&category3Id=&keyword=&brandId=&order=1
    @Operation(summary = "条件查询商品分页数据")
    @GetMapping("{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum")Integer pageNum,
                       @PathVariable("pageSize")Integer pageSize,
                       ProductSkuSearchDto productSkuSearchDto){
        //DTO接收查询条件
        PageInfo<ProductSku> page = productSkuService.getPageByCondition(pageNum,pageSize,productSkuSearchDto);
        //ProductSku分页数据
        return Result.ok(page);
    }
}
